package com.concerneyes.mine.TabActivity.OrderView;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.concerneyes.CEApp;
import com.concerneyes.R;
import com.concerneyes.mine.Task.OrderImpl;
import com.concerneyes.mine.Task.OrderListener;
import com.concerneyes.mine.adapter.MyOrderViewAdapter;
import com.concerneyes.mine.model.MyAdressModel;
import com.concerneyes.mine.model.OrderListModel;
import com.concerneyes.mine.model.OrderSubmit;
import com.concerneyes.pay.ConfirmInfoActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.AbsBaseView;
import common.PreferUtil;
import common.WebViewActivity;
import http.HttpAsyncTask;
import http.JsonMap;
import http.Url;
import util.ActivityUtil;
import util.ToastHelper;

/**
 * 我的订单列表
 */

public class MyOrderView extends AbsBaseView implements AdapterView.OnItemClickListener, MyOrderViewAdapter.Callback, OrderListener {
    private ListView listView;
    private MyOrderViewAdapter adapter;
    private String[] mOrderStyle;
    private Context context;
    private Resources resources;
    private String type;
    private int order_Style;
    private OrderImpl impl;
    private ImageView iv_empty;
    private List<OrderListModel> models = null;

    public MyOrderView(Context context, String orderStyle, String preson, int position) {
        super(context);
        this.context = context;
        resources = context.getResources();
        mOrderStyle = resources.getStringArray(R.array.order_me_style);
        listView = (ListView) findViewById(R.id.list_view_order);
        iv_empty = (ImageView) findViewById(R.id.iv_empty);
        listView.setOnItemClickListener(this);
        impl = new OrderImpl(this);
        adapter = new MyOrderViewAdapter(getContext(), this, orderStyle, preson);
        listView.setAdapter(adapter);
        type = preson;
        order_Style = position;
        loadData("");
    }

    @Override
    public int layoutResId() {
        return R.layout.view_my_order;
    }

    @Override
    public void loadData(Object data) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uid", PreferUtil.getUserId());
        params.put("status", String.valueOf(order_Style + 1));
        HttpAsyncTask http = new HttpAsyncTask() {
            @Override
            public void onStart(String taskId) {
                getActivity().showLoadingDialog(R.string.loading);
            }

            @Override
            public void onFinish(String taskId, String response) {
                getActivity().cancelLoadingDialog();
                JsonMap ret = JsonMap.parseJson(response);
                if (ret == null) {
                    ToastHelper.getInstance()._toast(R.string.network_error_tip);
                    return;
                }
                List<OrderListModel> model = null;
                if (ret.getInt("code") == 200) {
                    Log.e(this.getClass().getSimpleName(),"data_tostrings"+response);
                    String datas = ret.getListMap("data").toString();
                    try {
                        model = new Gson().fromJson(datas, new TypeToken<ArrayList<OrderListModel>>() {
                        }.getType());
                    } catch (Exception e) {
                    }

                    if (model == null || model.size() == 0) {
                        // 暂无数据
                        iv_empty.setVisibility(View.VISIBLE);
                        listView.setVisibility(View.GONE);
                        return;
                    }
                    models = model;
                    listView.setVisibility(View.VISIBLE);
                    iv_empty.setVisibility(View.GONE);
                    adapter.clear();
                    adapter.addDatas(model);
                } else {
                    ToastHelper.getInstance()._toast(ret.getString("msg"));
                }
            }
        };
        http.setDebug(true);
        http.excute(Url.BaseUrl + "/order/list", params, "POST");
    }


    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public void click(View v, String text) {
        int position = (Integer) v.getTag();
        switch (v.getId()) {
            case R.id.order_btn_left:
                intentDetail(String.valueOf(adapter.getItem(position).getId()));
                break;
            case R.id.order_btn_right:
                if (order_Style == 0) {
                    Bundle bundle = new Bundle();
                    bundle.putInt("orderId", models.get(position).getId());
                    ActivityUtil.setIntent(context, ConfirmInfoActivity.class, bundle);
                } else if (order_Style == 2) {
                    String expressNo = models.get(position).getExpressNo();
                    Bundle bundle = new Bundle();
                    bundle.putString("title", getResources().getString(R.string.log_mag));
                    if (!TextUtils.isEmpty(expressNo)) {
                        bundle.putString("url", Url.BaseUrl + "/kuaidi/search?nu=" + expressNo);
                    } else {
                        bundle.putString("url", Url.BaseUrl + "/kuaidi/search?nu=59655403387");
                    }
                    ActivityUtil.setIntent(context, CheckLogActivity.class, bundle);
                }
                break;

            default:
                break;
        }
    }

    private void intentDetail(String orderId) {
        Bundle bundle = new Bundle();
        bundle.putString("orderId", orderId);
        ActivityUtil.setIntent(context, MyOrderDetail.class, bundle);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        intentDetail(String.valueOf(adapter.getItem(position).getId()));
    }


    @Override
    public void onSubmitSuccess(OrderSubmit msg) {

    }

    @Override
    public void onSubmitFail() {

    }

    @Override
    public void oncompleteSuccess() {

    }

    @Override
    public void oncompleteFail() {

    }

    @Override
    public void onDeliverSuccess() {

    }

    @Override
    public void onDeliverFail() {

    }


}